<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="addYgInfor">新 增</el-button>
        <!-- <el-button type="primary" class="myBgGreen">导出Excel</el-button> -->
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">
      <el-col :span="7">
        <el-input  v-model="kw" placeholder="请输入关键字"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="list"
        :border = true
        :stripe = true
        :row-class-name="tableRowClassName"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          label="序号"
          width="100"
          type="index"
          :index="indexMethod"
          >
        </el-table-column>
       
        <el-table-column
          prop="role_name"
          label="角色名"
          >
        </el-table-column>
        <el-table-column
          prop="data_rule"
          label="角色数据权限"
          width="150"
          >
            <template scope="scope">
          <span>{{scope.row.data_rule | dataRule}}</span>
        </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="350">
          <template scope="scope">
            <el-button class="look" type="text" size="small" @click="onGoHomePower(scope.row.role_id)">首页权限</el-button> |
            <el-button class="look" type="text" size="small" @click="onGoMenuPower(scope.row.role_id)">菜单权限</el-button> |
            <el-button class="look" type="text" size="small" @click="onGoAppPower(scope.row.role_id)">APP权限</el-button> |
            <el-button class="look" type="text primary" size="small" @click="onOpenUpdateRole(scope.row.role_id)">角色编辑</el-button> |
            <el-button class="look" type="text warning" size="small" @click="onOpenDeleteRole(scope.row.role_id)">角色删除</el-button> 

            <!-- <el-button @click="getDetail(scope.row.role_id)" class="look" type="text" size="small">查看</el-button> |
            <el-button type="text" @click="updateInfor" class="modifly" size="small">编辑</el-button>  -->
          </template>
        </el-table-column>
      </el-table>
      <div class="block">                    
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page_num"
          :page-sizes="[10, 15]"
          :page-size="page_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-row>

    <el-dialog title="添加角色" class="myRoleDialog" :visible.sync="roleDialog">
      <el-row>
        <el-col :span="4" class="myLineHeight_40px">
          <span class="require">*</span>
          角色名:
        </el-col>
        <el-col :span="20">
          <el-input placeholder="输入角色名" v-model="role_name"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px">
        <el-col :span="4" class="myLineHeight_40px">
          <span class="require">*</span>
          数据权限:
        </el-col>
        <el-col :span="20">
          <el-select 
            v-model="data_rule"
            clearable
            placeholder="选择数据权限">
            <el-option
                v-for="item in dataPermissionsList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
          </el-select>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="roleDialog = false">取 消</el-button>
        <el-button type="primary" class="myBgGreen" v-if="operation=='add'" @click="onSubRole">提 交</el-button>
        <el-button type="primary" class="myBgGreen" v-if="operation=='update'" @click="onUpdateRole">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
    padding-bottom: 5px;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
</style>

<script>
  import { getRoleList , addRole , getRoleDetail , updateRole , deleteRole } from '@/api/role';
  export default {
    data(){
      return{
        title:"角色管理",
        loading: false,

        kw:"",
        list:[],

        page_size: null, //每页显示多少条
        page_num: null, //当前页码   
        total:null, //总条数  

        idArr:[],
        lookObj:{},
        multipleSelection: [],  
        options: [],

        roleDialog:false,
        data_rule:"",
        role_name:"",
        dataPermissionsList:[{value:"all",label:"可见全部"},{value:"own",label:"可见个人"}],
        operation:"",
        subId:""
      }
    },      
    created(){
      document.title=this.title;
      this.fetchRoleList();
    },
    filters: {
      dataRule: function(value) {
        if (value === 'all') {
          return '可见全部';
        } else if (value === 'own') {
          return '可见个人';
        }
      }
    },
    methods:{
      fetchRoleList() {
        this.loading = true;
        let vm = this;
        let params = {
          kw: this.kw,
          page_num: this.page_num,
          page_size: this.page_size
        }; 
        getRoleList(params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;
          // console.log(response.data.rows)
          vm.loading = false;
          
        }).catch(error => {
          vm.loading = false;
        })
      },
      fuzzySearch:function(){
        this.fetchRoleList();
      },
      handleSizeChange(val) {//每页条数控制
        this.page_size = val;
        this.fetchRoleList();
      },
      handleCurrentChange(val) {//每页
        let pageNum=window.localStorage.getItem("pageNum");
        if(pageNum){
          this.page_num = Number(pageNum);
          window.localStorage.removeItem("pageNum");
        }else{
          this.page_num = val;
        }
        this.fetchRoleList();
      },
      deleteData:function (data,index) {
        this.data = data;
        this.index = index;
        this.lookObj = data.list[index]
      },
      getDetail:function(roleId){
        this.$router.push({
          path:'/jsglDetailed',
          query: {type: 'info', id: roleId}
        })
      },
      sureDelete:function(event){
     
        var pageArr = {
          page_num : this.page_num
        }
        
        if(this.list.length==1){
          var pageArr = {
            page_num : this.page_num-1
          }
           this.page_num = this.page_num-1;
        }

        var arrId = {
          room_ids : this.idArr.join(",")
        }

        optVue.deleteJsData(this,this.index,Tips,pageArr,arrId);
      },
      
      addYgInfor:function(){
        this.role_name="";
        this.data_rule="";
        this.operation="add";
        this.roleDialog=true;
        // this.$router.push({path:'/jsglsy', query: {type: 'add'}})
      },
      updateInfor:function(){
        this.$router.push({path:'/jsglsy', query: {type: 'update'}})
      },
      saveData:function(){},
      modifyData:function(id){
        this.$router.push({path:'/jsglcg', query: {type: 'infor',id:id}})
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].role_id)
          }
        }else{
          this.idArr = [];
        }
      },
      tableRowClassName(row, index) {
          //把每一行的索引放进row
          row.index = index
      },
      indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      },
      onSubRole(){//保存角色
        let vm=this;
        if(!vm.data_rule || !vm.role_name){
          vm.$message.error("执行失败！请将数据填写完整！");
          return;
        }
        let params={
          data_rule:vm.data_rule,
          role_name:vm.role_name
        }
        addRole(params).then(response => {
          let res=response.data;
          vm.$message.success("执行成功！");
          vm.fetchRoleList();
          vm.roleDialog=false;
          // console.log(res)
        }).catch(error => {
          vm.$message.error("执行失败!");
          // console.log(error)
        })
      },
      onOpenUpdateRole(r_id){//拉取角色信息
        let vm=this;
        vm.operation="update";
        vm.subId=r_id;
        getRoleDetail(r_id).then(response => {
          let res=response.data;
          vm.roleDialog=true;
          vm.role_name=res.role_name;
          vm.data_rule=res.data_rule;
          // console.log(res)
        }).catch(error => {
          // console.log(error)
        })
      },
      onUpdateRole(){//更新角色信息
        let vm=this;
        let params={
          data_rule:vm.data_rule,
          role_name:vm.role_name
        }
        updateRole(vm.subId,params).then(response => {
          let res=response.data;
          vm.$message.success("执行成功！");
          vm.fetchRoleList();
          vm.roleDialog=false;
          // console.log(res)
        }).catch(error => {
          vm.$message.error("执行失败!");
          // console.log(error)
        })
      },
      onOpenDeleteRole(r_id){//删除角色
        let vm=this;
        vm.$confirm('您确定要删除这条数据吗？', '提示', {//弹出框
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params={
            role_ids:r_id
          }
          deleteRole(params).then(response => {
            let res=response.data;
            vm.$message.success("执行成功！");
            vm.roleDialog=false;
            vm.fetchRoleList();
          }).catch(error => {
            vm.$message.error("执行失败！"+error.response.data.error_msg);
          })
        }).catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消执行'
          });          
        });
      },
      onGoHomePower(r_id){//跳转首页权限页面
        this.$router.push({
          path:"/homePower",
          query:{
            id:r_id,
            pageNum: this.page_num
          }
        })
      },
      onGoMenuPower(r_id){//跳转菜单权限页面
        this.$router.push({
          path:"/menuPower",
          query:{
            id:r_id,
            pageNum: this.page_num
          }
        })
      },
      onGoAppPower(r_id){//跳转菜单权限页面
        this.$router.push({
          path:"/appPower",
          query:{
            id:r_id,
            pageNum: this.page_num
          }
        })
      }
    }

  }


</script>



